package cool.taomu.toolkit.provider

import com.google.common.util.concurrent.RateLimiter
import com.google.inject.Inject
import javax.inject.Named
import javax.inject.Provider
import org.slf4j.LoggerFactory

class RateLimiterProvider implements Provider<RateLimiter> {
    
    val static LOG = LoggerFactory.getLogger(RateLimiterProvider);

	@Inject(optional=true)
	@Named("taomu.rateLimiter")
	Double rateLimiter

	override get() {
		if (rateLimiter === null) {
			val coreNumber = Runtime.getRuntime().availableProcessors;
			rateLimiter = coreNumber * 50.0;
		}
	    LOG.info("设置限流:{}",rateLimiter)
		RateLimiter.create(rateLimiter)
	}

}
